package com.c2soft.nepalcmx.controller;


import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.log4j.Log4j2;

import org.apache.log4j.Category;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;








import com.c2soft.nepalcmx.command.AccountDetailCommand;
import com.c2soft.nepalcmx.command.ConfirmTradeCommand;
import com.c2soft.nepalcmx.command.TradeBookCommand;
import com.c2soft.nepalcmx.services.AccountDetailService;
import com.c2soft.nepalcmx.services.TradeBookService;
import com.c2soft.nepalcmx.util.JsonUtil;



@Log4j2
@Controller
@RequestMapping("/user")
public class AccountDetailController {
	
	@Autowired
	private AccountDetailService accountDetailService;
	
	@RequestMapping(value = "/account", method={RequestMethod.POST,RequestMethod.GET}, headers = "Accept=application/json")
	public @ResponseBody String account( @RequestBody String json,HttpServletRequest req) {
		String brokerId=(String) req.getSession().getAttribute("UsrId");
		AccountDetailCommand account= (AccountDetailCommand) JsonUtil.fromJson(json,AccountDetailCommand.class);
		account.setBrokerId(brokerId);
       boolean a=accountDetailService.accountService(account);
		if(a==true){
		return "{\"msg\":\"Account Details added successfully.\"}";
		}
		else{
			return "{\"msg\":\"User's Account Detail can't be kept as Basic Details are not saved yet.\"}";
		}
	}
	
	@RequestMapping(value = "/brokerAccount", method=RequestMethod.POST, headers = "Accept=application/json")
	@ResponseBody
	public String brokerAccount( @RequestBody String json) {
		
		AccountDetailCommand account= (AccountDetailCommand) JsonUtil.fromJson(json,AccountDetailCommand.class);
		boolean a=accountDetailService.brokerAccountService(account);
        
		if(a==true){
		return "{\"msg\":\"Account Details added successfully.\"}";
		}
		else{
			return "{\"msg\":\"Broker's Account Detail can't be kept as Basic Details are not saved yet.\"}";
		}
		
	}
	
	@RequestMapping(value = "/accountX", method=RequestMethod.POST, headers = "Accept=application/json")
	@ResponseBody
	public String exchangeAccount( @RequestBody String json) {
		
		AccountDetailCommand account= (AccountDetailCommand) JsonUtil.fromJson(json,AccountDetailCommand.class);
		
		accountDetailService.exchangeAccountService(account);
        
		return "{\"msg\":\"Account Details added successfully.\"}";
		
	}
	
	
	
@RequestMapping(value = "/userAccountDetail",method=RequestMethod.GET, headers = "Accept=application/json")
	
	public @ResponseBody List<AccountDetailCommand> getAccountDetail(@RequestParam("userId") String userId) {
		List<AccountDetailCommand> accountDetail=accountDetailService.getUserAccounts(userId);
		return accountDetail;
	}

	
	@RequestMapping(value = "/accountDetail",method=RequestMethod.GET, headers = "Accept=application/json")
	
	public @ResponseBody List<AccountDetailCommand> getAccountDetail(HttpServletRequest req) {
		String brokerId=(String) req.getSession().getAttribute("UsrId");
		List<AccountDetailCommand> account=accountDetailService.getAccountDetail(brokerId);
		return account;
	}
	
	
	
	
	@RequestMapping(value = "/updateMngUser",method={RequestMethod.POST,RequestMethod.GET}, headers = "Accept=application/json")

	public @ResponseBody List<AccountDetailCommand> updateUserData(HttpServletRequest req,@RequestBody String userId) {
		String brokerId=(String) req.getSession().getAttribute("UsrId");
		List<AccountDetailCommand> users=accountDetailService.getAccounts1(userId,brokerId);
		return users;
	}
	
	@RequestMapping(value = "/updateAccount", method=RequestMethod.POST, headers = "Accept=application/json")
	@ResponseBody
	public String updateAccount( @RequestBody String json,HttpServletRequest req) {
		
		String brokerId=(String) req.getSession().getAttribute("UsrId");
		AccountDetailCommand account= (AccountDetailCommand) JsonUtil.fromJson(json,AccountDetailCommand.class);
        account.setBrokerId(brokerId);
        accountDetailService.updateAccountService(account);
		return "{\"msg\":\"Account Details Updated successfully.\"}";
	}
	
	@RequestMapping(value = "/fenlgr", method={RequestMethod.POST,RequestMethod.GET}, headers = "Accept=application/json")
	public @ResponseBody List<AccountDetailCommand> fenlgr( @RequestBody String json,HttpServletRequest req) 
			throws ParseException {
		String userId=(String) req.getSession().getAttribute("UsrId");
		ConfirmTradeCommand confirmTrade= (ConfirmTradeCommand) JsonUtil.fromJson(json,ConfirmTradeCommand.class);
	    List<AccountDetailCommand> accountDetail=accountDetailService.getAccountEntries(userId, confirmTrade);
	    return accountDetail;
	    }
	@RequestMapping(value = "/bills", method={RequestMethod.POST,RequestMethod.GET}, headers = "Accept=application/json")
	public @ResponseBody List<AccountDetailCommand> bills( @RequestBody String json,HttpServletRequest req) 
			throws ParseException {
		String userId=(String) req.getSession().getAttribute("UsrId");
		ConfirmTradeCommand confirmTrade= (ConfirmTradeCommand) JsonUtil.fromJson(json,ConfirmTradeCommand.class);
	    List<AccountDetailCommand> accountDetail=accountDetailService.getBillsEntries(userId, confirmTrade);
	    return accountDetail;
	    }
}
